﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace _1.EnglishAndChinese
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }





        private void Form1_Load(object sender, EventArgs e)
        {
            MySqlConnection connection = new MySqlConnection(
                "datasource=localhost;username=root;" +
                "password=123456;database=word_db;charset=utf8");
            connection.Open();


            //设置查询命令
            //MySqlCommand cmd = new MySqlCommand("SELECT * FROM English", connection);
            //查询结果读取器
            MySqlCommand  mycom = connection.CreateCommand();
            mycom.CommandText = "SELECT *FROM English";

            MySqlDataReader reader = mycom.ExecuteReader();


         
            try
            {
                int i = 0;
                
                while (reader.Read())
                {
                    Console.WriteLine(reader[0].ToString() + " " + reader[1].ToString());
                    Label label = new Label();
                    label.Size = new Size(100, 30);
                    label.Text = reader[0].ToString();
                    panel1.Controls.Add(label);

                    TextBox textBox = new TextBox();
                    textBox.Size = new Size(100, 30);
                    panel1.Controls.Add(textBox);

                    label.Location = new Point(0, i * 30);
                    textBox.Location = new Point(150, i * 30);
                    

                    Label ans = new Label();
                    ans.Text = reader[1].ToString();
                    ans.Hide();

                    KeyEventHandler handler = (sender1, e1) => {
                        if (e1.KeyCode == Keys.Enter)
                        {
                            if (textBox.Text == ans.Text)
                            {
                                MessageBox.Show("正确", "提示");
                            }
                            else
                            {
                                MessageBox.Show("错误", "提示");
                            }
                            
                        }
                    };
                    textBox.KeyUp += handler;

                    ++i;
                }

            }
            catch (Exception ex) { }
            finally
            {
                reader.Close();
                connection.Close();
            }

        }

        private void textBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Control || e.KeyCode == Keys.Enter)
            {
                MessageBox.Show("看到此提示说明在textbox1内按了回车键", "textbox控件绑定回车控件");
            }
        }
    }

    
}
